<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>14 同分排名</title>
    <style>
        h1 {
            text-align: center;
        }
    </style>
</head>

<body>
    <h1>14 同分排名</h1>
</body>
<script>
    var data = [
        {
            id: 104,
            math: 68,
            name: '赵今麦'
        },
        {
            id: 101,
            math: 90,
            name: '张三'
        },
        {
            id: 102,
            math: 88,
            name: '李四'
        },
        {
            id: 103,
            math: 90,
            name: '王五'
        },
        {
            id: 103,
            math: 78,
            name: '宋雨琦'
        }
    ];

    // 根据数学成绩排序
    data.sort(function (n1, n2) {
        return n2.math - n1.math;
    });

    console.log(data);

    // 把数学成绩存到数组里
    var math = [];
    data.forEach(function (item) {
        math.push(item.math);
    });
    console.log(math);

    // 数组去重
    function norepeat(arr) {
        var newarr = []; // 存去重后的数据
        arr.forEach(function (item) {
            if (newarr.indexOf(item) == -1) {
                // 没有
                newarr.push(item);
            }
        })
        return newarr;
    }

    var newmath = norepeat(math); // 去重后的数据 [90,88,78]

    // 添加排名
    data.forEach(function(item) {
        newmath.forEach(function(val, index) {
            if (item.math == val) {
                item.rank = index + 1;
            }
        });
    });

    console.log(data);

</script>

</html>